################################################################################
## Luke Taylor
## Thermal Conductance Measurement Automation
################################################################################
## Revisions
## 08/10/2010 
##      - integration with keithley class and lock-in class
##      - took quite a bit of time to try and get the Prologix controller
##          to address the different GPIB units and integrate this functionality
##          with the existing classes.  gave up as of 10:30pm
## 08/09/2010 
##      - beg
################################################################################
## Packages
################################################################################
import serial
import k_meter_lnt
from pylab import plot, show, figure
from numpy import savetxt
from scipy import stats
from easygui import enterbox
from time import sleep
################################################################################
## Globals
################################################################################
vcom = 9
keith_gpib = 24
wdir = 'C:\\Users\\luke\\Documents\\My Dropbox\\UMN_research\\pythons\\optical_coatings\\heat_parameters\\%s'
alpha = 0.0007  # resistor temperature coefficient
################################################################################
## Subroutines
################################################################################
def meas_sweep(start,stop,step,nstep,vprot=10,delay=0.1,spacing='LIN'):
    keith.raw.write('*RST')
    sleep(10)
    keith.raw.write(':SYST:BEEP:STAT OFF')
    keith.raw.write(':FORM:ELEM VOLT, CURR')
    keith.raw.write(':SENS:FUNC:CONC OFF')
    keith.raw.write(':SOUR:FUNC CURR')
    keith.raw.write(':SENS:FUNC "VOLT:DC"')
    keith.raw.write(':SENS:VOLT:PROT 10')
    keith.raw.write(':SOUR:CURR:START ' + str(start))
    keith.raw.write(':SOUR:CURR:STOP ' + str(stop))
    keith.raw.write(':SOUR:CURR:STEP ' + str(step))
    keith.raw.write(':SOUR:CURR:MODE SWE')
    keith.raw.write(':SOUR:SWE:RANG AUTO')
    keith.raw.write(':SOUR:SWE:SPAC ' + spacing)
    keith.raw.write(':TRIG:COUN ' + str(nstep))
    keith.raw.write(':SOUR:DEL ' + str(delay))
    keith.raw.write(':OUTP ON')
    data = keith.raw.ask(':READ?')
    keith.raw.write(':OUTP OFF')
    return data
    
################################################################################
## Main
################################################################################
filename = enterbox(msg='enter csv filename', title='choose file', default='conductance.csv')
vcom_port = serial.Serial(vcom-1)
keith = k_meter_lnt.sourcemeter(keith_gpib,vcom_port)
print keith.raw.ask('*idn?')
data = meas_sweep(1E-6,100E-6,1E-6,100)
datalist = data.split(',')
vdatalist = datalist[:len(datalist):2]
idatalist = datalist[1:len(datalist):2]
vdata = [float(v) for v in vdatalist]
idata = [float(i) for i in idatalist]
yaxis = [idata[i]/vdata[i] for i in range(len(idata))]
xaxis = [i**2.0 for i in idata]
power = [idata[i]*vdata[i] for i in range(len(idata))]
volts = vdata
res = [vdata[i]/idata[i] for i in range(len(idata))]
cond = yaxis
current = idata
curr2 = xaxis
## plot(res,current,'b+')
## plot(volts,current,'b+')
data = [[current[i],volts[i],curr2[i],cond[i],res[i],power[i]] for i in range(len(current))]
fid = open(wdir %filename,'w')
fid.write('current,voltage,current2,conductance,resistance,power\n')
savetxt(fid,data,delimiter=',',fmt='%1.4e')
fid.close()
plot(curr2,cond,'b+')
show()
vcom_port.close()
################################################################################
## EOF
################################################################################